/**
 * 加载处理主方法
 */
const vscode = require('vscode');
const fs = require('fs');
const vscodePath = require('./vscodePath')
const { addScript, deleteScript } = require('./htmlMerge');
const { loadResources, removeResources } = require('./loadResources')
const vsHelp = require('./vsHelp')

const init = (type) => {
   // 获取文件内容
   const html = fs.readFileSync(vscodePath.vsHtml, 'utf-8')
   /** 加载资源 */
   loadResources()
   /** 在主文件添加资源路径 */
   const newHtml = addScript(html)
   /** 替换主文件 */
   fs.writeFileSync(vscodePath.vsHtml, newHtml, 'utf-8')

   if ('start' === type) {
      vsHelp.showInfoRestart('萌娘启动成功，请重新启动！')
   } else if ('updata' === type) {
      vsHelp.showInfoRestart('萌娘参数修改，请重新启动！')
   }
}

/**
 * 卸载操作
 */
const unInstall = () => {
   // 获取文件内容
   const html = fs.readFileSync(vscodePath.vsHtml, 'utf-8')
   /** 删除资源 */
   removeResources()
   /** 在主文件删除资源路径 */
   const newHtml = deleteScript(html)
   /** 替换主文件 */
   fs.writeFileSync(vscodePath.vsHtml, newHtml, 'utf-8')

   vsHelp.showInfoRestart('萌娘插件卸载成功，请重新启动！')
}

const watch = () => {
   init('start')
   return vscode.workspace.onDidChangeConfiguration(() => init('updata'));
}

module.exports = {
   init,
   watch,
   unInstall
}